{% extends "base.html" %}
{%- macro render_handin_form(lang) -%}
  <form name="handin-{{ lang }}" action="{{ url_for('homework', slug=hw.slug) }}#handins"
        method="POST" enctype="multipart/form-data" role="form">
    {{ forms[lang].hidden_tag() }}
    <div style="display: hidden;">
      <input type="hidden" name="handin_lang" value="{{ lang }}">
    </div>
    {% for field in forms[lang] -%}
      {% if field.name != 'csrf_token' -%}
      <div class="form-group{% if field.errors %} has-error{% endif %}">
        {{ field.label(class_="control-label") }}
        {% if field.type == 'StringField' -%}
          {{ field(class_='form-control') }}
        {%- else -%}
          {{ field }}
        {%- endif %}
        {% if field.errors -%}
          {% for e in field.errors %}<p class="help-block text-danger">{{ e }}</p>{% endfor %}
        {%- endif %}
      </div>
      {%- endif %}
    {%- endfor %}
    <div class="pull-right">
      <button type="submit" class="btn btn-default">{{ _('Submit') }}</button>
    </div>
  </form>
{%- endmacro -%}
{% block subtitle -%}
{{ hw.info.name }} - {{ _('Homework') }}
{%- endblock %}
{% block content -%}
  <h3 class="hw-heading">
    {{ hw.info.name }}
    <span class="pull-right">
      <a href="{{ url_for('hwhandins', slug=hw.slug) }}" class="btn btn-primary">{{ _('View Submissions') }}</a>
    </span>
  </h3>
  
  <!-- Homework description here -->
  <div class="hw-desc panel panel-default">
    <div class="panel-heading">{{ _('Description') }}</div>
    <div class="panel-body">{{ hw.info.formatted_desc | safe }}</div>
  </div>

  {% if current_user.is_admin and hw.info.formatted_solve -%}
    <!-- Homework solution here -->
    <div class="hw-solve panel panel-default">
      <div class="panel-heading">{{ _('Solution') }}</div>
      <div class="panel-body">{{ hw.info.formatted_solve | safe }}</div>
    </div>
  {%- endif %}

  <!-- Homework attachments here -->
  {% if hw.count_attach() > 0 -%}
    <div class="hw-attach panel panel-default">
      <div class="panel-heading">{{ _('Attachments') }}</div>
      <div class="panel-body">
        {% for c in hwlangs -%}
          {% if hw.get_code(c).has_attach -%}
            <div class="lang">
              <a href="{{ hw.attach_url(c) }}">{{ c | codelang }}</a>
              <span class="size">({{ hw.attach_size(c) | sizeformat }})</span>
            </div>
          {%- endif %}
        {%- endfor %}
      </div>
    </div>
  {%- endif %}

  <!-- Homework deadlines here -->
  {% set next_ddl = hw.get_next_deadline() -%}
  {%- set ddl_text_class = 'default' if next_ddl else 'danger' -%}
  <div class="hw-deadlines panel panel-{{ ddl_text_class }} text-{{ ddl_text_class }}">
    <div class="panel-heading">{{ _('Deadlines') }}</div>
    <div class="panel-body">
      {%- if not next_ddl -%}
        {{ _('This homework is already out of date.') }}
      {%- else -%}
        {{ _('This homework will reach next deadline in '
             '<span style="color: %(duecolor)s">%(due)s</span>, '
             'and the score will be scaled by '
             '<span style="color: %(scalecolor)s">%(scale)s</span>.',
             due=next_ddl[0] | timedelta, scale=next_ddl[1],
             duecolor=next_ddl[0] | duecolor, scalecolor=next_ddl[1] | scalecolor)
           | safe }}
      {%- endif -%}
    </div>
    <table class="table table-hover">
      <tr>
        <th>{{ _('Due date') }}</th>
        <th>{{ _('Score scale ') }}</th>
      </tr>
      {% for ddl in hw.deadlines %}
        {%- if ddl[0] < g.utcnow -%}
          <tr class="text-muted">
            <td>{{ ddl[0] | datetimeformat }}</td>
            <td>{{ ddl[1] }}</td>
          </tr>
        {%- else -%}
          <tr{% if next_ddl and ddl[0] == next_ddl[0] %} class="info"{% endif %}>
            <td style="color: {{ ddl[0] | duecolor }}">{{ ddl[0] | datetimeformat }}</td>
            <td style="color: {{ ddl[1] | scalecolor }}">{{ ddl[1] }}</td>
          </tr>
        {%- endif -%}
      {% endfor %}
    </table>
  </div>

  <!-- Upload handins here -->
  {% if next_ddl -%}
  <div class="hw-handins panel panel-default" id="handins">
    <div class="panel-heading">{{ _('Submission') }}</div>
    {% if (hwlangs | count) > 1 -%}
    <div class="panel-body container-fluid">
      <div class="row">
        <!-- Select the code language to upload handin -->
        <div class="col-md-3">
          <ul class="nav nav-pills nav-stacked" role="tablist">
            {% for lang in hwlangs -%}
              <li{% if lang == active_lang %} class="active"{% endif %}>
                <a href="#handin-{{ lang }}" role="tab" data-toggle="tab">{{ lang | codelang }}</a>
              </li>
            {%- endfor %}
          </ul>
        </div>
        <!-- upload form areas -->
        <div class="col-md-9 tab-content">
          <div class="tab-pane " id="home">...</div>
          {% for lang in hwlangs -%}
            <div class="tab-pane{% if lang == active_lang %} active{% endif %}" id="handin-{{ lang }}">
              {{ render_handin_form(lang) }}
            </div>
          {%- endfor %}
        </div>
      </div>
    {%- else -%}
      <div class="panel-body">
        {%- set lang = hwlangs[0] -%}
        {{ render_handin_form(lang) }}
      </div>
    {%- endif %}
    </div>
  </div>
  {%- endif %}
{%- endblock %}